package cz.apnetis.fitweb.server.gae;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

public class GaePersistenceManagerFactoryHelper {

  private static PersistenceManagerFactory pmf;
  private static PersistenceManagerFactory proxy;
  
  public static PersistenceManagerFactory getPersistenceManagerFactory() {
    if (proxy!=null)
      return proxy;
    pmf = JDOHelper.getPersistenceManagerFactory("transactions-optional");
    InvocationHandler handler = new InvocationHandler() {
      @Override
      public Object invoke(Object newProxy, Method method, Object[] args)
          throws Throwable {
        return method.invoke(pmf, args);
      }
    };
    proxy = (PersistenceManagerFactory)
    Proxy.newProxyInstance(pmf.getClass().getClassLoader(), new Class[] {
      PersistenceManagerFactory.class }, handler);
    return proxy;
  }
}